<?php


namespace app\api\controller\v1;


use app\api\service\AppToken;
use app\api\service\Token;
use app\api\service\Usertoken;
use app\api\service\ValidataSensitive;
use app\api\service\YiLianYun;
use app\api\validate\AppTokenGet;
use app\api\validate\checkSensitive;
use app\api\validate\getToken;
use app\lib\exception\parameterException;

class TokenController{

    //获取token令牌
    public function getToken($code =''){
        (new getToken())->goCheck();
        $userToken = new Usertoken($code);
        $token = $userToken->getOpenid();
        return json(['token'=>$token]);
    }

    //验证token
    public function verifyToken($token){
        if(!$token){
            throw  new parameterException(['msg'=>'token不能为空']);
        }

        $tokenservice = new Token();
        $res = $tokenservice::verifyToken($token);
        return json(['isValid'=>$res]);
    }

    //敏感词汇的验证
    public function checkSensitive($content){
        (new checkSensitive())->goCheck();
        $checkMsg = new ValidataSensitive();
        $result = $checkMsg->checkMsg($content);
        if($result['errcode'] !=0){
            if($result['errcode'] == 87014){
                return $result['risky'];
            }
        }
        return json(['msg'=>'不存在敏感词汇']);
    }

    /**
     * 第三方应用获取令牌
     * @url /app_token?
     * @POST ac=:ac se=:secret
     */
    public function getAppToken($ac='', $se='')
    {
        header('Access-Control-Allow-Origin: *');
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
        header('Access-Control-Allow-Methods: GET');
        (new AppTokenGet())->goCheck();
        $app = new AppToken();
        $token = $app->get($ac, $se);
        return json([
            'token' => $token
        ]);
    }




}












